/**
 * 
 */
package com.effectiv.req1.business.impl;

import java.util.List;
import java.util.Locale;

import jodd.util.RandomStringUtil;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.security.authentication.encoding.PasswordEncoder;
import org.springframework.stereotype.Service;

import com.effectiv.req1.business.api.AbstractBusinessService;
import com.effectiv.req1.business.api.UserService;
import com.effectiv.req1.domain.Company;
import com.effectiv.req1.domain.Lov;

import com.effectiv.req1.domain.User;
import com.effectiv.req1.event.RegistrationEvent;
import com.effectiv.req1.event.UserActivationEvent;
import com.effectiv.req1.repository.LovRepository;
import com.effectiv.req1.repository.UserRepository;

/**
 * @author Dhrubo
 * 
 */
@Service
@Slf4j
public class UserServiceImpl extends AbstractBusinessService<User, String>
		implements UserService {

	@Autowired
	private UserRepository repository;

	@Autowired
	private LovRepository lovRepository;

	@Autowired
	private PasswordEncoder encoder;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.effectiv.req1.business.api.UserService#findUsersByCompany(com.effectiv
	 * .req1.domain.Company)
	 */
	@Override
	public List<User> findUsersByCompany(Company company) {

		if (company == null) {
			company = getCompany();
		}
		return repository.findByCompany(company);
	}

	@Override
	public List<User> findByCompanyAndNameLike(Company company, String name) {
		if (company == null) {
			company = getCompany();
		}
		return repository.findByCompanyAndNameLike(company, name);
	}

	@Override
	public void register(User user, Locale locale) {
		String random = RandomStringUtil.randomAlpha(8);

		log.info("###### Password = {}", random);

		String pwd = encoder.encodePassword(random, null);

		user.setPassword(pwd);
		Lov role = lovRepository.findByName("ROLE_COMPANY_ADMIN");
		user.setRole(role);

		log.info("###### Registering user = {}", user);

		super.save(user);

		user.setPassword(random);
		RegistrationEvent event = BeanUtils
				.instantiate(RegistrationEvent.class);
		event.setUser(user);
		event.setLocale(locale);

		post(event);

	}

	@Override
	public void save(User t) {

		String random = RandomStringUtil.randomAlpha(8);
		String pwd = encoder.encodePassword(random, null);

		t.setPassword(pwd);
		Lov role = lovRepository.findByName("ROLE_EMPLOYEE");
		t.setRole(role);
		t.setCompany(getCompany());
		super.save(t);

		t.setPassword(random);
		RegistrationEvent event = BeanUtils
				.instantiate(RegistrationEvent.class);
		event.setUser(t);
		// TODO --- Change must
		event.setLocale(Locale.getDefault());

		post(event);
	}

	@Override
	protected void initInternal() {
		setRepository(repository);
	}

	@Override
	public User activate(String id, Locale locale) {
		User user = repository.findOne(id);

		if (user != null) {

			user.setActive(true);
			super.update(user);

			UserActivationEvent event = BeanUtils
					.instantiate(UserActivationEvent.class);
			event.setUser(user);
			event.setLocale(locale);

			post(event);

		}

		return user;
	}

}
